﻿function GetDaysFromSelectedDate()
{
  var start = new Date(deDate.GetDate()),
  				end = new Date(),
  				diff = new Date(start - end ),
  				days = diff / 1000 / 60 / 60 / 24;
  return Math.round(days);
}

function mainTabChanged(s, e) {
	var x = e;
	if (e.tab.index == 1)
		grdRoutes.Refresh();
	if (e.tab.index == 2)
		cbpChart.PerformCallback();
}

function getDepartureAirportCode()
{
	var fromGrid = grdFrom.GetGridView();
	if(fromGrid != null)
	{
		fromGrid.GetRowValues(fromGrid.GetFocusedRowIndex(), 'AirportCode', OnGetFromGridRowValues);
	}
}

function OnGetFromGridRowValues(Value)
{
	$(document).ready(function () {
		$.simpleWeather({
			location: Value,
			woeid: '',
			unit: 'f',
			success: function (weather) {
				var days = GetDaysFromSelectedDate();
				if (days == 0) {
					html = '<h2><i class="icon-' + weather.code + '"></i> ' + weather.temp + '&deg;' + weather.units.temp + '</h2>';
					//html += '<ul><li>' + weather.city + ', ' + weather.region + '</li>';
					html += '<ul><li class="currently">' + weather.currently + '</li>';
					html += '<li>' + weather.wind.direction + ' ' + weather.wind.speed + ' ' + weather.units.speed + '</li>';
					html += '<a href=' + weather.link + ' target="_blank">View ' + weather.city + ' forecast</a></ul>';
					setCookie('tempDeparture', weather.temp, 1)
					setCookie('windSpeedDeparture', weather.wind.speed, 1);
				} else {
					html = '<h2><i class="icon-' + weather.forecast[days].code + '"></i> ' + weather.forecast[Math.round(days)].low + '/' + weather.forecast[Math.round(days)].high + '&deg;' + weather.units.temp + '</h2>';
					//html += '<ul><li>' + weather.city + ', ' + weather.region + '</li>';
					html += '<ul><li>' + weather.forecast[days].text + '</li>';
					html += '<a href=' + weather.link + ' target="_blank">View ' + weather.city + ' forecast</a></ul>';
					setCookie('tempDeparture', weather.forecast[Math.round(days)].low, 1)
					setCookie('windSpeedDeparture', 0, 1);
				}
			
				$("#weatherDepart").html(html);
			},
			error: function (error) {
				$("#weatherDepart").html('<p>' + error + '</p>');
			}
		});
	});
}

function getDestinationAirportCode() {
	var toGrid = grdTo.GetGridView();
	if (toGrid != null) {
		toGrid.GetRowValues(toGrid.GetFocusedRowIndex(), 'AirportCode', OnGetToGridRowValues);
	}
}

function OnGetToGridRowValues(Value) {
	$(document).ready(function () {
		$.simpleWeather({
			location: Value,
			woeid: '',
			unit: 'f',
			success: function (weather) {

				var days = GetDaysFromSelectedDate();
				if (days == 0) {
					html = '<h2><i class="icon-' + weather.code + '"></i> ' + weather.temp + '&deg;' + weather.units.temp + '</h2>';
					//html += '<ul><li>' + weather.city + ', ' + weather.region + '</li>';
					html += '<ul><li class="currently">' + weather.currently + '</li>';
					html += '<li>' + weather.wind.direction + ' ' + weather.wind.speed + ' ' + weather.units.speed + '</li>';
					html += '<a href=' + weather.link + ' target="_blank">View ' + weather.city + ' forecast</a></ul>';
					setCookie('windSpeedArrival', weather.wind.speed, 1);
					setCookie('tempArrival', weather.units.temp, 1)
				} else {
					html = '<h2><i class="icon-' + weather.forecast[days].code + '"></i> ' + weather.forecast[Math.round(days)].low + '/' + weather.forecast[Math.round(days)].high + '&deg;' + weather.units.temp + '</h2>';
					html += '<ul><li>' + weather.forecast[days].text + '</li>';
					html += '<a href=' + weather.link + ' target="_blank">View ' + weather.city + ' forecast</a></ul>';
					setCookie('windSpeedArrival', weather.forecast[Math.round(days)].low, 1);
					setCookie('tempArrival', weather.forecast[Math.round(days)].low, 1)
				}
	
				$("#weatherArrive").html(html);
			},
			error: function (error) {
				$("#weatherArrive").html('<p>' + error + '</p>');
			}
		});
	});
}
function processDateChange()
{

	getDepartureAirportCode();
	getDestinationAirportCode();
}
function setCookie(cname, cvalue, exdays) {
	var d = new Date();
	d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
	var expires = "expires=" + d.toGMTString();
	document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1);
		if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
	}
	return "";
}

